package main;

import connectivity.DbManager;
import connectivity.QueryManager;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;

import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import view.Header;


/**
 *
 * @author Administrator
 */
public final class BarApplication extends Header{

    /**
     * Define frame width, height and name
     */
    public static final int FRAME_WIDTH = 960;
    public static final int FRAME_HEIGHT = 600;
    public static final String NAME = "Fitshape Sportscholen";
    public static final String CURRENCY = "€ ";
    /**
     * static fonts which are used within the application
     */
    public static final Font FONT_10_PLAIN = new Font("Verdana", Font.PLAIN, 10);
    public static final Font FONT_10_BOLD = new Font("Verdana", Font.BOLD, 10);
    public static final Font FONT_12_BOLD = new Font("Verdana", Font.BOLD, 12);
    public static final Font FONT_16_BOLD = new Font("Verdana", Font.BOLD, 16);
   
    private Header header;
    /**
     * database manager
     */
    private DbManager dbManager;
    private QueryManager queryManager;
    
    /**
     * models used in the application
     */
    private model.Basket basket;
    /**
     * the main window
     */
    private JFrame mainWindow;
    /**
     * singleton of the application
     */

    //private JLabel headerHome = new JLabel("home");;
    // private JPanel Header = new JPanel();
    private static BarApplication instance = new BarApplication();
    
   

    public BarApplication() {
    }

    private void initComponents() {
    }

    public void initialize() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            System.err.println("Error setting LookAndFeelClassName: " + e);
        }
        // create and initialize the connectivity
        dbManager = new DbManager();
        dbManager.openConnection();
        queryManager = new QueryManager(dbManager);

        // create an empty basket
        basket = new model.Basket();
 
    }

    public void startup() {
        
        mainWindow = new JFrame(NAME);
        mainWindow.setSize(FRAME_WIDTH, FRAME_HEIGHT);
           // centering the JFrame on screen
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = mainWindow.getSize();
        if (frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
        }
        if (frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
        }
        mainWindow.setLocation((screenSize.width - frameSize.width) / 2,
                (screenSize.height - frameSize.height) / 2);

        /**
         * Make the window closing [x] button on the frame active
         */
        mainWindow.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent event) {
                shutdown();
            }
        });

        mainWindow.getContentPane().setLayout(new BorderLayout());


        showPanel(new view.LoginScreen());

        mainWindow.setLocationRelativeTo(null);
        mainWindow.setVisible(true);
    }

    public void showPanel(JPanel panel) {

        header = new Header();    

        mainWindow.getContentPane().removeAll();
        header = new Header();
  
        
        mainWindow.getContentPane().add(header, BorderLayout.NORTH);
        mainWindow.getContentPane().add(panel, BorderLayout.CENTER);
        mainWindow.getContentPane().validate();
        mainWindow.getContentPane().repaint();

    }

    public void exit() {
        mainWindow.setVisible(false);
        shutdown();
        System.gc();
        java.awt.Window win[]= java.awt.Window.getWindows();
        for (int i = 0; i < win.length; i++) {
            win[i].dispose();
            win[i] = null;
        }
    }

    public void shutdown() {
        mainWindow.dispose();
        dbManager.closeConnection();
    }

    /**
     * @return the instance of this class
     */
    public static BarApplication getInstance() {
        return instance;
    }

    /**
     * @return the queryManager
     */
    public static QueryManager getQueryManager() {
        return getInstance().queryManager;

    }


    /**
     * @return the basket
     */
    public static model.Basket getBasket() {
        return getInstance().basket;
    }
    

    //main method-----------------------------------------------------------------
    public static void main(String args[]) {
        final BarApplication applicatie = BarApplication.getInstance();
       // LoginScreen loginScreen = new LoginScreen();
        //loginScreen.getGebruikersNummer();
        //loginScreen.getSoortRecht();
        
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    applicatie.initialize();
                    applicatie.startup();
                } catch (Exception e) {
                    System.out.println("Application" + applicatie.getClass().getName() + "failed to launch");
                }
            }
        });
       
    }
      
}
